<?php
class CommonAction extends Action {
   //传递参数 
    protected $paras=array();

    // 查询数据
    public function index(){
        
        
      $name=  $this->getActionName();
        $Form    = M($name);
        import("@.ORG.Page"); //导入分页类
        $count = $Form->count();    //计算总数
        $p = new Page ( $count, 5 );
       // $list    =    $Form->limit(5)->order('id desc')->findAll();
       $list=$Form->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
        $this->assign('list',$list);
        $page = $p->show (); 
        $this->assign('page',$page);
        $this->display();
    }

      //数据写入
      function insert() {
        //B('FilterString');
        $name=$this->getActionName();
        $model = D ($name);
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        //保存当前数据对象
        $list=$model->add ();
        if ($list!==false) { //保存成功
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success ('新增成功!');
        } else {
            //失败提示
            $this->error ('新增失败!');
        }
    }
    //数据编辑
    function edit() {
        $name=$this->getActionName();
        $model = M ( $name );
        $id = $_REQUEST [$model->getPk ()];
        $vo = $model->getById ( $id );
        $this->assign ( 'vo', $vo );
        $this->display ();
    }
    //数据更新
    function update() {
        //B('FilterString');
        $name=$this->getActionName();
        $model = D ( $name );
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        // 更新数据
        $list=$model->save ();
        if (false !== $list) {
            //成功提示
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success ('编辑成功!');
        } else {
            //错误提示
            $this->error ('编辑失败!');
        }
    }
    //数据删除
    public function delete() {
        //删除指定记录
        $name=$this->getActionName();
        $model = M ($name);
        if (! empty ( $model )) {
            $pk = $model->getPk ();
            $id = $_REQUEST [$pk];
            if (isset ( $id )) {
                $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
                $list=$model->where ( $condition )->delete();
                if ($list!==false) {
                    $this->success ('删除成功！' );
                } else {
                    $this->error ('删除失败！');
                }
            } else {
                $this->error ( '非法操作' );
            }
        }
    }   
    //生成多查询条件
    function buildWhere()
    {
       $w="";
       $qtypes=preg_split("/,/",$this->P("qtype"));
       $querys= preg_split("/,/",$this->P("query"));
       for($i=0;$i<count($qtypes);$i++){
         $w.="{$qtypes[$i]} like '%{$querys[$i]}%' and "; 
       }
       if(substr($w,strlen($w)-4,4)=="and ")
       $w=substr($w,0,strlen($w)-4); 
       //print_r($w);
       return $w;
        
    }
    
    //生成FlexGrid数据
    public function buildJSON($data,$primaryKey,$page,$total)
    {
        
        header("Content-type:text/html;charset=utf-8");   //这句很重要,防止乱码  
        $json = "";
        $json .= "{";
        $json .= "page: $page,";
        if($total)
        $json .= "total: $total,";
        else
        $json .= "total: 0,"; 
        $json .= "rows: [";
        $rc = false;
        $j=count($data)-1;
        // $pk=$Form->getPk()?$Form->getPk():$Form->pk;
        $pk=$primaryKey?$primaryKey:"id";
        for($i=0;$i<=$j;$i++)
        {
           // $json .= ",";
            $json .= "{";
            $json .= "id:'".$data[$i][$pk]."',"; 
            $json .= "cell:[";              
            foreach ($data[$i] as $key => $value) {
            $json .= "'".addslashes($value)."',";
            }
            if(substr($json,strlen($json)-1,1)==",")
            $json=substr($json,0,strlen($json)-1);
            $json .="]"."},";  
        
        }
        if($j>=0)  //这句也很重要
        $json=substr($json,0,strlen($json)-1);
        $json .= "]";
        $json .= "}";
        return $json;
        
    }
    
    
    
    //生成json数据
    public function  json()
    {
        
        /*
        $page = $_POST['page'];
        $rp = $_POST['rp'];
        $sortname = $_POST['sortname'];
        $sortorder = $_POST['sortorder'];  
        $qtype=$_POST["qtype"]; 
        $query=$_POST["query"];
        */
       
        $page=$this->P('page');
        $rp=$this->P('rp');
        $sortname=$this->P('sortname');
        $sortorder=$this->P('sortorder');
        $qtype=$this->P('qtype');
        $query=$this->P('query');
        
        if(!$sortorder) $sortorder="asc";
     
        if(!$sortname) $sortname="id"; 
            
        if(!$page) $page=1;  
        
        if(!$rp ) $rp =10; 
        
         
        
         $name=  $this->getActionName();
        $Form    = M($name); 
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
            $list=$Form->where($map)->limit(($page-1)*$rp.','.$rp)->order("$sortname $sortorder")->findAll(); 
           $total=$Form->where($map)->count();
             
        }
        else 
        if($query&&$qtype)
        {
            
           $list=$Form->where($this->buildWhere())->limit(($page-1)*$rp.','.$rp)->order("$sortname $sortorder")->findAll(); 
           //$list=$Form->where("$qtype like '%{$query}%'")->limit(($page-1)*$rp.','.$rp)->order("$sortname $sortorder")->findAll(); 
           $total=$Form->where($this->buildWhere())->count();
          // $total=$Form->where("$qtype like '%{$query}%'")->count();
          
        
        }
        else
        {
        
        $total=$Form->count();
        $list=$Form->limit(($page-1)*$rp.','.$rp)->order("$sortname $sortorder")->findAll();
        
     
       
        
        
        }

          header("Content-type:text/html;charset=utf-8");   //这句很重要,防止乱码  
       echo $this->buildJSON($list,$Form->pk,$page,$total); 
        
    }
    
      //flexgrid视图
        function view()
    {
       $this->display();
    }
    
     //初始化操作
     
       public function __construct()
       {
           $this->initParas(); 
           parent::__construct();
          
       }    
    
    

    //初始化参数
    
    function initParas()
    {
       
   
       if(count($_POST)>0) 
        foreach ($_POST as $key => $value) {
            
            $this->paras[strtolower($key)]=addslashes($value);
        
        }
        
        if(count($_GET)>0) 
        foreach ($_GET as $key => $value) {
            
            $this->paras[strtolower($key)]=addslashes($value);
            
        }
    }
    
    //取得参数
    
    public function P($paraName)
    {
        return $this->paras[$paraName];
    }
    
 
 
}
?>



